package ar.edu.unlu.sistemas.p2p.business.strategy.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

import ar.edu.unlu.sistemas.p2p.business.index.IndexController;
import ar.edu.unlu.sistemas.p2p.business.peer.PeerController;
import ar.edu.unlu.sistemas.p2p.business.strategy.ProcessQueryStrategy;
import ar.edu.unlu.sistemas.p2p.business.util.Logger;
import ar.edu.unlu.sistemas.p2p.business.util.StringUtils;
import ar.edu.unlu.sistemas.p2p.business.vo.QueryResultVO;

public class ProcessQueryBooleanModeStrategyImpl extends ProcessQueryStrategy {

	@Override
	public Collection<QueryResultVO> execute(String query) {
		try {
			String expr = StringUtils.processLogicalExpression(query);
			Collection<QueryResultVO> queryResults = IndexController.getInstance().processBoolQuery(expr);
			Iterator<QueryResultVO> allDocs = queryResults.iterator();
			while (allDocs.hasNext()) 
				allDocs.next().setAddress(PeerController.getInstance().getLocalPeer().getAddress());
			return this.filterResults(queryResults);
		} catch (Exception e) {
			Logger.logError(this.getClass().getSimpleName(), " Hubo un error al procesa query booleana ", e);
			return new ArrayList<QueryResultVO>();
		}
	}

}
